#ifndef MPDBACKEND_H
#define MPDBACKEND_H

#include <QObject>
#include <QtPlugin>
#include <QString>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtCore/QObject>
#include <qdebug.h>
#include <QTimer>
#include <QtDBus>
#include <QMap>
#include <QDBusConnection>
#include <QDBusReply>
#include <QDBusArgument>
#include <QVariantMap>
#include <QVariant>
#include "../../../src/song.h"
#include "../../../src/audioplayerbackend.h"

struct DBusStatus
{
    int Play; //Playing = 0, Paused = 1, Stopped = 2
    int Random; //Linearly = 0, Randomly = 1
    int Repeat; //Go_To_Next = 0, Repeat_Current = 1
    int RepeatPlaylist; //Stop_When_Finished = 0, Never_Give_Up_Playing = 1
};

Q_DECLARE_METATYPE(DBusStatus)

// Marshall the DBusStatus data into a D-BUS argument
QDBusArgument &operator << ( QDBusArgument &argument, const DBusStatus &status );
// Retrieve the DBusStatus data from the D-BUS argument
const QDBusArgument &operator >> ( const QDBusArgument &argument, DBusStatus &status );

class AmarokBackend: public AudioPlayerBackend
{
    Q_OBJECT
    Q_INTERFACES(AudioPlayerBackend)
public:
    AmarokBackend();
    uint features();
    QString getCurrentSongTitle();
    QString getCurrentSongArtist();
    QString getCurrentSongAlbum();
    uint getVolume();
    bool isPlaying();
    bool isPaused();

    QList<QString> getAllArtists();
    QList<QString> getAllAlbums();
    QList<Song*> * getAllSongs();
    QList<QString> getAlbumsFromArtist(QString artist);
    QList<Song*> * getSongsFromAlbum(Album * album);
    QList<Song*> * getSongsFromArtist(Artist * artist);

private:
    QDBusMessage amarok_playpause;
    QDBusMessage amarok_pause;
    QDBusMessage amarok_stop;
    QDBusMessage amarok_play;
    QDBusMessage amarok_prev;
    QDBusMessage amarok_next;
    QDBusMessage amarok_repeat;
    QDBusMessage amarok_volumeset;
    QDBusMessage amarok_volumeget;
    QDBusMessage amarok_getmetadata;
    QDBusMessage amarok_volumeup;
    QDBusMessage amarok_volumedown;
    QDBusMessage amarok_mute;
    QDBusMessage amarok_forward;
    QDBusMessage amarok_backward;
    QDBusMessage amarok_getstatus;

    DBusStatus currentStatus;


signals:
    void randomStatusChanged(bool status);
    void repeatStatusChanged(bool status);
    void songChanged();
    void stateChanged(bool playing);

public slots:

    bool stop();
    bool next();
    bool prev();

    bool setVolume(int value);
    bool seek(int sec);
    int getElapsedTime();

    bool play();
    bool play(uint id);
    bool play(Song * song);
    bool pause();
    bool togglePause();

    bool setRandom(bool status);
    bool getRandom();
    bool toggleRandom();

    bool setRepeat(bool status);
    bool getRepeat();
    bool toggleRepeat();

private slots:
    void timerTimeout();
    void statusChange(DBusStatus status);
    void trackChanged(QVariantMap metadata);
};

#endif // MPDBACKEND_H
